package com.mall.controller.protal;

import com.github.pagehelper.PageInfo;
import com.mall.common.Const;
import com.mall.common.MySessionContext;
import com.mall.common.ResponseCode;
import com.mall.common.ServerResponse;
import com.mall.pojo.Cart;
import com.mall.pojo.Order;
import com.mall.pojo.Shipping;
import com.mall.pojo.User;
import com.mall.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by xiaoyao on 2017/8/31.
 */
@Controller
@RequestMapping("/order/")
public class OrderController {

    @Autowired
    private IOrderService iOrderService;

    /**
     * 生成订单
     * @param shippingId
     * @return
     */
    @RequestMapping("create.do")
    @ResponseBody
    public ServerResponse<Order> create(Integer shippingId){
        HttpSession session = MySessionContext.getSession(Const.sessionId );
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iOrderService.createOrder(user.getId(),shippingId);
    }

    /**
     * 前端处理了购物车，多参数创建订单
     * @param shippingId
     * @return
     */
    @RequestMapping("createParamOrder.do")
    @ResponseBody
    public ServerResponse<Order> createParamOrder(BigDecimal orderPrice, Integer shippingId, String cartIds){
        HttpSession session = MySessionContext.getSession(Const.sessionId );
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iOrderService.createParamOrder(user.getId(),shippingId,cartIds,orderPrice);
    }

    /**
     * 取消订单
     * @param orderNo
     * @return
     */
    @RequestMapping("cancel.do")
    @ResponseBody
    public ServerResponse cancel(long orderNo){
        HttpSession session = MySessionContext.getSession(Const.sessionId );
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iOrderService.cancelOrder(user.getId(),orderNo);
    }


    /**
     * 获取购物车中已选商品的订单视图
     * @return
     */
    @RequestMapping("getCartCheckedProductOrder.do")
    @ResponseBody
    public ServerResponse getCartCheckedProductOrder(){
        HttpSession session = MySessionContext.getSession(Const.sessionId );
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iOrderService.getCartCheckedProductOrder(user.getId());
    }

    /**
     * 订单列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("list.do")
    @ResponseBody
    public ServerResponse<PageInfo> list(
                                         @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                         @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        HttpSession session = MySessionContext.getSession(Const.sessionId );
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iOrderService.getlist(user.getId(),pageNum,pageSize);
    }

    /**
     * 订单详情
     * @param orderNo
     * @return
     */
    @RequestMapping("detail.do")
    @ResponseBody
    public ServerResponse detail(long orderNo){
        //判断是否登录
        if(Const.sessionId == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请先登录");
        }
        HttpSession session = MySessionContext.getSession(Const.sessionId );
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return iOrderService.getdetail(user.getId(),orderNo);
    }


}
